package com.baizhi.wj.service;

import com.baizhi.wj.dao.CategoryMapper;
import com.baizhi.wj.dto.CategoryDTO;
import com.baizhi.wj.entity.Category;
import com.baizhi.wj.entity.CategoryExample;
import com.baizhi.wj.ov.AddVO;
import com.baizhi.wj.ov.CategoryOV;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    //第一级分页查询
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public CategoryOV queryByPage(CategoryDTO categoryDTO) {
            CategoryExample example = new CategoryExample();
            Map<String, Object> map = new HashMap<String, Object>();
            int offset = (categoryDTO.getPage() - 1) * categoryDTO.getPageSize();
            int limit = categoryDTO.getPageSize();
            RowBounds rowBounds = new RowBounds(offset, limit);

            example.createCriteria().andLevelsEqualTo(1);
            List<Category> list = categoryMapper.selectByExampleAndRowBounds(example,rowBounds);
            int i = categoryMapper.selectCountByExample(example);
            CategoryOV categoryOV = new CategoryOV();
            categoryOV.setPage(categoryDTO.getPage());
            categoryOV.setRows(list);
            categoryOV.setTotal(i);
            return categoryOV;
    }

    //第二级分页查询
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public CategoryOV queryTwoPage(CategoryDTO categoryDTO) {
        CategoryExample example = new CategoryExample();
        Map<String, Object> map = new HashMap<String, Object>();
        int offset = (categoryDTO.getPage() - 1) * categoryDTO.getPageSize();
        int limit = categoryDTO.getPageSize();
        RowBounds rowBounds = new RowBounds(offset, limit);

        example.createCriteria().andLevelsEqualTo(2);
        List<Category> list = categoryMapper.selectByExampleAndRowBounds(example,rowBounds);
        int i = categoryMapper.selectCountByExample(example);
        CategoryOV categoryOV = new CategoryOV();
        categoryOV.setPage(categoryDTO.getPage());
        categoryOV.setRows(list);
        categoryOV.setTotal(i);
        return categoryOV;

    }

    @Override
    public void insert(Category category) {
            category.setLevels(1);
            categoryMapper.insertSelective(category);

    }
    //类别添加第一级
    @Override
    public Map<String,Object> insiger(@RequestBody Category category) {
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            Integer parentId = category.getParentId();
            if (parentId==null){
                category.setLevels(1);
            }else {
                category.setLevels(2);
            }
            System.out.println(category.getCateName());
            categoryMapper.insertSelective(category);
            map.put("massage","添加成功");
            map.put("status",200);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("massage","添加失败");
            map.put("status",400);
            return map;
        }
    }
    //类别删除
    @Override
    public Map<String,Object> delete(Category category) {
        Map<String,Object> map =new HashMap<String,Object>();
            try {
                Integer id = category.getId();
                Integer parentId = category.getParentId();
                if (parentId!=null) {
                    category.setLevels(id);
                    map.put("massage","删除成功");
                    map.put("status",200);
                    return map;
                }
                CategoryExample example = new CategoryExample();
                example.createCriteria().andIdEqualTo(id).andParentIdEqualTo(category.getId());
                int count = categoryMapper.selectCountByExample(example);
                if (count>0){
                    map.put("massage","无法删除");
                    map.put("status",400);
                }else {
                    categoryMapper.deleteByPrimaryKey(id);
                    map.put("massage","删除成功");
                    map.put("status",200);

                }
                return map;
            }catch (Exception e){
                e.printStackTrace();
                map.put("massage","删除失败");
                map.put("status",400);
                return map;
            }
    }



    //二级添加的下拉列表
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Category> selectAll(int  levels) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(levels);
        return categoryMapper.selectByExample(categoryExample);
    }
    //类别查一个
    @Override
    public Category selectOne(int id) {
        return categoryMapper.selectByPrimaryKey(id);
    }
    //类别的修改
    @Override
    public Map<String,Object> update(Category category) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            categoryMapper.updateByPrimaryKey(category);
            map.put("massage","修改成功");
            map.put("status",200);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("massage","修改失败");
            map.put("status",400);
            return map;
        }

    }
}

